<?php

namespace App\Http\Controllers\Admins;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Jenson\BaseAdmin\Controllers\JensonBaseAdminController;
use Jenson\BaseAdmin\Libraries\Helper as BaseAdminHelper;
use Jenson\BaseAdmin\Models\Menu;
use Jenson\Core\Libraries\Helper as CoreHelper;

class HomeController extends Controller
{
    /**
     * @param Request $request
     * @return RedirectResponse
     *
     * 入口
     */
    public function admins(Request $request): RedirectResponse
    {
        return BaseAdminHelper::isLogin($request);
    }

    public function homePage(){
        CoreHelper::LoadConfig();
        return redirect(route('admin.default'));
    }

    /**
     * @param Request $request
     * @return Application|Factory|View
     */
    public function index(Request $request)
    {
        $title = '工作台';
        $admin = $request->get('admin');//dd($admin);
//        if($admin['is_activation'] != 1){
//            #账号激活页面
//            return redirect('www.baidu.com');//->route('users.education.home');
////            return view('admin.home',compact('title'));
//        }
        return view('v1.admin.home',compact('title'));
    }

    /**
     * 控制台首页(增加权限)
     *
     * @param Request $request
     * @return Application|Factory|View
     */
    public function default(Request $request){
        $admin = $request->get('admin');
//        if($admin['is_activation'] != 1){
//            #账号激活页面
//            return view('v1.admin.account_activation');
////            return redirect()->route('admin.account.activation');
//        }
        $status = "home";
        # 获取管理员菜单信息
//        $menu_ids = AdminRoles::query()->where('alias',$admin['roles'])->value('menus');
        $menu_ids = '';
        #顶级菜单
        $columns = ["id","name","link","sort","parent_id","group_id","i_ico_class"];
        $where = [
            'parent_id'=>0
        ];
        $query = Menu::query()->where($where);
        if($menu_ids) {
            $menu_ids = explode(',', $menu_ids);
            $query->whereIn('id', $menu_ids);
        }
        $menus = $query->latest('sort')->get($columns)->toArray();
        #临时菜单数组
        $tempMenus = [];
        #重新整理数据
        foreach ($menus as $menu){
            $tempMenus[$menu["id"]] = $menu;
            $tempMenus[$menu["id"]]['hasChild'] = 0;
            $tempMenus[$menu["id"]]['isFather'] = 1;
        }
        #子菜单数据
        unset($columns['group_id']);
        unset($columns['i_ico_class']);
//        $columns = ["id","name","link","sort","parent_id"];
        $subMenus = Menu::query()->where('parent_id','<>',0)->latest('sort')->get($columns)->toArray();
        foreach ($subMenus as $menu){
            $tempMenus[$menu["parent_id"]]['nodes'][] = $menu;
            $tempMenus[$menu["parent_id"]]['hasChild'] = 1;
        }
        if(config('mbcore_baseadmin.baseadmin_menuGroup')){
            $echoMenus = [];
            foreach($tempMenus as $menu){
                $echoMenus[$menu["group_id"]][] = $menu;
            }
            //dd($echoMenus);
        }else{
            $echoMenus = $tempMenus;
        }
        # 读取数据库的权限信息
        $jenson = new JensonBaseAdminController($request);
        $jenson->setRolesArr($request);
        return view('mbcore.baseadmin::admin.index',['status' => $status,'menus'=>$echoMenus]);
    }
}
